package com.zf.zfed.audit.ui

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.TextView
import com.zf.zfed.audit.R
import com.zf.zfed.audit.data.SignalType

class SignalTypeAdapter(
    context: Context,
    private val signalTypes: List<SignalType>
) : ArrayAdapter<SignalType>(context, R.layout.item_signal_type, signalTypes) {

    private var selectedPosition = -1

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view = convertView ?: LayoutInflater.from(context)
            .inflate(R.layout.item_signal_type, parent, false)
        
        val signalType = signalTypes[position]
        
//        val iconView = view.findViewById<ImageView>(R.id.iv_signal_type_icon)
        val nameView = view.findViewById<TextView>(R.id.tv_signal_type_name)
        
//        iconView.setImageResource(signalType.iconResId)
        nameView.text = signalType.name
        
        return view
    }

    override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
        val view = convertView ?: LayoutInflater.from(context)
            .inflate(R.layout.item_signal_type, parent, false)
        
        val signalType = signalTypes[position]
        val isSelected = position == selectedPosition
        
//        val iconView = view.findViewById<ImageView>(R.id.iv_signal_type_icon)
        val nameView = view.findViewById<TextView>(R.id.tv_signal_type_name)
        
        // 设置背景
        if (isSelected) {
            view.setBackgroundResource(R.drawable.bg_spinner_item_selected)
            nameView.setTextColor(context.getColor(android.R.color.white))
            // 选中时图标也变白色
//            iconView.setColorFilter(context.getColor(android.R.color.white))
        } else {
            view.setBackgroundResource(R.drawable.bg_spinner_item_normal)
            nameView.setTextColor(context.getColor(android.R.color.black))
            // 未选中时图标恢复原色
//            iconView.clearColorFilter()
        }
        
//        iconView.setImageResource(signalType.iconResId)
        nameView.text = signalType.name
        
        return view
    }

    fun setSelectedPosition(position: Int) {
        selectedPosition = position
        notifyDataSetChanged()
    }
}
